<?php

namespace Home\Controller;

use Utuo\ForegroundController;

class HouseController extends ForegroundController {
	
	// 详细页
	public function showAction() {
		$d = D("ModelHouse");
		$id = I('id', 0, 'intval'); // 文章id
		$content = $d->detail(null, $id);
		$this->assign("content", $content['data']);
		$configUrl = sprintf('http://%s%s',$_SERVER['HTTP_HOST'],__CONTROLLER__.'/getConfigXml/id/'.$id.'/cat_id/'.$this->catId);
		$this->assign("configUrl",$configUrl);
		layout(false);
		$this->display($this->getTemplete($content['model']['tag'], 'content'));
	}
	
	public function thumbAction() {
		if(!IS_SAE){
			return false;
		}
		Header( "Content-type: image/jpeg"); //2.JPEG格式
		$maxWidth = 300;
		$maxHeight = 300;
		$path = I('path');
		$s = new \SaeStorage();
		$arr = explode('/', ltrim($path, './'));
		$domain = array_shift($arr);
		$source = implode('/', $arr);
		$source_url = $s->getUrl($domain, $source);
		if (!$s->fileExists($domain, $source)){
			return false;
		}
		$thumbFilePath = $arr;
		$thumbFileName = array_pop($thumbFilePath);
		$thumbFilePath[] = "min_".$thumbFileName;
		$thumbFilePath = implode('/', $thumbFilePath);
		if($s->fileExists($domain, $thumbFilePath)){
			$thumb_source_url = $s->getUrl($domain, $thumbFilePath);
			$temp_img = imagecreatefromjpeg($thumb_source_url);
		}else{
			$info=getimagesize($source_url);
			$width=$info[0];//获取图片宽度
			$height=$info[1];//获取图片高度
			$temp_img = imagecreatetruecolor($maxWidth,$maxHeight); //创建画布
			$im = imagecreatefromjpeg($source_url);
			imagecopyresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
			//imagecopyresampled($temp_img,$this->img,0,0,$_cut_width,$_cut_height,$new_width,$new_height,$this->width,$this->height);
			$write = true;
		}
		ob_start();
		imagejpeg($temp_img);
		if($write){
			$imgstr = ob_get_contents();
			$s->write($domain, $thumbFilePath, $imgstr);
		}
		ob_end_flush();
		imagedestroy($temp_img);
	}
	
	// 获取3d场景配置
	public function getConfigXmlAction() {
		$id = I('id', 0, 'intval');
		$pics = M('ModelHouseConfig')->where('id=%d', $id)->getField('pic');
		$pics = unserialize($pics);
		$pics_thumb = array();
		foreach ($pics as $v){
			$pics_thumb[] = sprintf('http://%s%s',$_SERVER['HTTP_HOST'],__CONTROLLER__.'/thumb/?path='.$this->getLocalPic($v));
		}
		$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<panorama id="">
	<view fovmode="0" pannorth="0">
		<start pan="0" fov="70" tilt="0" />
		<min pan="0" fov="5" tilt="-90" />
		<max pan="360" fov="120" tilt="90" />
	</view>
	<userdata title="" datetime="" description=""
		copyright="" tags="" author="" source="" comment="" info="" longitude=""
		latitude="" />
	<hotspots width="180" height="20" wordwrap="1">
		<label width="180" backgroundalpha="1" enabled="1" height="20"
			backgroundcolor="0xffffff" bordercolor="0x000000" border="1"
			textcolor="0x000000" background="1" borderalpha="1" borderradius="1"
			wordwrap="1" textalpha="1" />
		<polystyle mode="0" backgroundalpha="0.2509803921568627"
			backgroundcolor="0x0000ff" bordercolor="0x0000ff" borderalpha="1" />
	</hotspots>
	<media />
	<input
		tile0url="{$pics[1]}"
		tile1url="{$pics[2]}"
		tile2url="{$pics[3]}"
		tile3url="{$pics[4]}"
		tile4url="{$pics[0]}"
		tile5url="{$pics[5]}"
		prev5url="{$pics_thumb[5]}"
		prev4url="{$pics_thumb[0]}"
		prev3url="{$pics_thumb[4]}"
		prev2url="{$pics_thumb[3]}"
		prev1url="{$pics_thumb[2]}"
		prev0url="{$pics_thumb[1]}"
		tilesize="750"
		tilescale="1.013333333333333"  />
	<control simulatemass="1" lockedmouse="0" lockedkeyboard="0"
		dblclickfullscreen="0" invertwheel="0" lockedwheel="0" invertcontrol="1"
		speedwheel="1" sensitivity="8" />
</panorama>
XML;
		
		header('Content-Type:text/xml; charset=utf-8');
		exit($xml);
	}
	
	private function getLocalPic($url){
		return preg_replace("/http:\/\/[^\/]+/", C("UPLOAD_DOMAIN"), $url);
	}
}